package com.example.myapplication;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;

public class UserDataBaseHelper {
  private SQLiteDatabase database;
  private SqliteHelper databaseHelper;

  public UserDataBaseHelper(Context context) {
    databaseHelper = new SqliteHelper(context);

  }
  public void Open() {
    database = databaseHelper.getWritableDatabase();

  }

  public void Close() {
    databaseHelper.close();
  }

  public int InsertSingleUser(UserEntity user) {
    if (!IsUserExist(user.userAccount)) {
      //只需要插入User的账号密码用户名即可
      ContentValues values = new ContentValues();
      values.put("userAccount", user.getUserAccount());
      values.put("userPassword", user.getUserPassword());
      values.put("nickName", user.getUserName());

      int number = (int) database.insert(SqliteHelper.USER_TABLE, null, values);
      if (number > 0) {
        System.out.println("输入成功");
        return 1;
      } else {
        System.out.println("输入失败");
        return -1;
      }
    }
    return 0;
  }

  public int CheckUserPassword(String account, String password) {
    String[] projection = { "userPassword" };
    String selection = "useraccount=?";
    String[] selectionArgs = { account };
    Cursor cursor =
        database.query(SqliteHelper.USER_TABLE, projection, selection, selectionArgs, null, null,
            null);
    if (cursor.getCount()==0) {
      return -1;//说明用户不存在
    }
    String userPassword = "";
    while (cursor.moveToNext()) {
      int index = cursor.getColumnIndex("userPassword");
      userPassword = cursor.getString(index);
    }
    if (userPassword.equals(password)) {
      return 1;//说明正确
    } else {
      return 0;//说明密码错误
    }

  }

  public boolean IsUserExist(String account) {
    String[] projection = { "useraccount" };
    String selection = "useraccount=?";
    String[] selectionArgs = { account };
    Cursor cursor =
        database.query(SqliteHelper.USER_TABLE, projection, selection, selectionArgs, null, null,
            null);
    int number = cursor.getCount();
    cursor.close();
    if (number != 0) {
      return true;
    }
    return false;
  }

  public String GetUserNameByAccount(String account) {
    String[] projection = { "nickName" };
    String selection = "useraccount=?";
    String[] selectionArgs = { account };
    Cursor cursor =
        database.query(SqliteHelper.USER_TABLE, projection, selection, selectionArgs, null, null,
            null);
    cursor.moveToFirst();
    int index = cursor.getColumnIndex("nickName");
    return cursor.getString(index);
  }
  public void DeleteTable() {
    databaseHelper.DeleteTable();
    System.out.println("已经成功删除表格");
  }

  public void CreateNoteTable() {
    database.execSQL(
        "CREATE TABLE Notes(noteID INTEGER PRIMARY KEY AUTOINCREMENT,authorName VARCHAR(20) not null ,authorAccount VARCHAR(20) not null ,title VARCHAR(20),createtime TIMESTAMP not null DEFAULT CURRENT_TIMESTAMP,updatetime TIMESTAMP not null DEFAULT CURRENT_TIMESTAMP,content LONGTEXT,favour INTEGER DEFAULT 0)");
    System.out.println("创建表格成功");
  }
}
